<template>
  <div v-for="(item, index) in course" :key="item.id" class="home-card" @click="() => route.push({ path: '/main/detail', query: { id: item.id }})">
    <div class="header-info">
      <p>{{ item.semester }}</p>
      <h3 class="name">{{ item.name }}</h3>
      <p class="description">{{ item.classesInfo }}</p>
      <div class="qrcode">
        <img :src='image.add_course' alt="">
        <span>加课码:{{ item.addCourseCode }}</span>
      </div>
    </div>
    <div class="content-info"></div>
    <div class="user-info">
      <div class="left">
        <span class="tag-flag" v-if="item.topped === 1">学</span>
        <span style="cursor: pointer;">负责人:{{ item.principal }}</span>
      </div>

      <div class="right">
        <div @click.stop="() => {handelUpdateUpState(item.ownId, 0)}" v-if="item.topped === 1" style="cursor: pointer">取消置顶</div>
        <div @click.stop="() => {handelUpdateUpState(item.ownId, 1)}" v-else style="display: flex;cursor: pointer">
          <img :src="image.up" style="margin-top: -3px; margin-right: 5px; width: 18px; height: 18px; cursor: pointer">
          置顶
        </div>
        <el-dropdown placement="top" trigger="click" @click.stop="() => {}">
          <img :src="image.and_so" alt="" @click.stop="() => {}">
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item @click.stop="() => {handelDelete(item.ownId)}">退课</el-dropdown-item>
              <el-dropdown-item @click.stop="() => {handelFiled(item.ownId, 1)}">归档</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </div>
  </div>
</template>
<script setup>
import {image} from "../../assets/index.js";
import {useRouter} from "vue-router";
import {deleteCourse, updateUpState} from "../../api/stuCourse.js";
import {ElMessage} from "element-plus";
import {onMounted} from "vue";
const route = useRouter()
const emit = defineEmits(['initData'])

const props = defineProps({
  course: {
    type: Array
  },
  id: {
    type: String
  }
})

const handelUpdateUpState = (id, state) => {
  updateUpState({id: id, topped: state}).then(res => {
    if (res.code === 200) {
      message('success', '操作成功')
      emit('initData')
    }
  }, error => {
    message('error', error.message)
  })
}

const handelDelete = (id) => {
  deleteCourse({id: id}).then(res => {
    if (res.code === 200) {
      message('success', '删除成功')
      emit('initData')
    }
  }, error => {
    message('error', error.message)
  })
}

const handelFiled = (id, state) => {
  updateUpState({id: id, filed: state, topped: 0}).then(res => {
    if (res.code === 200) {
      message('success', '操作成功')
      emit('initData')
    }
  }, error => {
    message('error', error.message)
  })
}

const message = (type, text) => {
  ElMessage({
    message: text,
    type: type,
    plain: true,
  })
}

onMounted(() => {
})
</script>

<style scoped>

span {
  font-family: PingFangSC, PingFangSC-Medium, sans-serif;
}

.home-card {
  width: 318px;
  height: 255px;
  background: #fff;
  border: 1px solid #e2e6ed;
  border-radius: 8px;
  overflow: hidden;
  position: relative;
  padding: 0;
  outline: none;
  font-weight: 400;
  vertical-align: baseline;
  font-family: Roboto, Helvetica, Arial, sans-serif;
  -webkit-tap-highlight-color: rgba(0, 0, 0, 0);

  p {
    opacity: .6;
    font-size: 12px;
  }

  .header-info {
    cursor: pointer;
    padding: 18px 24px;
    height: 152px;
    color: #fff;
    background-repeat: no-repeat;
    background-size: cover;
    background-position: 50%;
    background-color: #3367d5;

    .name {
      margin-top: 3px;
      font-size: 18px;
      font-family: PingFangSC, PingFangSC-Medium, sans-serif;
      font-weight: 500;
      text-align: left;
      color: #fff;
      line-height: 32px;
    }

    .description {
      opacity: 1;
      font-family: PingFangSC, PingFangSC-Medium, sans-serif;
      font-weight: 500;
      text-align: left;
      color: #fff;
      line-height: 24px;
    }

    .qrcode {
      margin-top: 18px;
      display: flex;
      position: relative;
      font-size: 14px;
      font-family: PingFangSC, PingFangSC-Medium, sans-serif;
      font-weight: 500;
      text-align: left;
      color: #fff;
      line-height: 16px;

      img {
        display: block;
        width: 14px;
        margin-right: 8px;
        border: none;
      }

      span {
        font-size: 14px;
        outline: none;
      }
    }
  }

  .content-info {
    cursor: pointer;
    height: 60px;
    padding: 0 20px 16px 20px;
  }

  .user-info {
    display: flex;
    justify-content: space-between;
    height: 40px;
    padding: 8px 14px 15px;
    font-size: 14px;
    line-height: 16px;
    font-weight: 400;
    vertical-align: baseline;
    -webkit-font-smoothing: antialiased;
    font-family: Roboto, Helvetica, Arial, sans-serif;

    .left {
      display: flex;
      align-items: center;

      .tag-flag {
        background-color: #fff;
        border: 1px solid #4285f4;
        color: #4285f4;
        min-width: 20px;
        max-width: 50px;
        height: 20px;
        line-height: 18px;
        display: inline-block;
        border-radius: 2px;
        font-size: 14px;
        text-align: center;
        margin-right: 8px;
        font-family: Roboto, Helvetica, Arial, sans-serif;
      }
    }

    .right {
      display: flex;
      align-items: center;

      span {
        cursor: pointer;
        display: flex;
        align-items: center;
      }

      img {
        cursor: pointer;
        margin-left: 11px;
        display: block;
        width: 14px;
      }
    }
  }
}

</style>
